package com.dream.keigezhushou.Activity.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.dream.keigezhushou.Activity.Fragment.PlayFragment;
import com.dream.keigezhushou.Activity.acty.play.VideoInfoActivity;
import com.dream.keigezhushou.Activity.bean.Video;
import com.dream.keigezhushou.Activity.view.CircleImageView;
import com.dream.keigezhushou.Activity.wigdt.VideoConstant;
import com.dream.keigezhushou.R;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

/**
 * VideoAdapter
 */

public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.MyViewHolder> {

    private Context mContext;
    private List<Video> dataList;

    private PlayFragment.MyItemClickListener myItemClickListener;
    public VideoAdapter(Context context,List<Video> dataList) {

        this.mContext = context;
        this.dataList = dataList;
    }

    public void setOnItemClickListener(PlayFragment.MyItemClickListener clickListener){
        this.myItemClickListener = clickListener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_play_item, parent, false);
        MyViewHolder mHolder = new MyViewHolder(view);

        return mHolder;
    }


    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {

        Video videoBean = dataList.get(position);

//        holder.video.setUp(VideoConstant.videoUrl[position], JCVideoPlayer.SCREEN_LAYOUT_LIST);
        holder.video.setUp(videoBean.getUrl(),JCVideoPlayer.SCREEN_LAYOUT_LIST);

//        Picasso.with(holder.video.getContext())
//                .load(VideoConstant.videoThumbs[position])
//                .into(holder.video.thumbImageView);
        
        holder.tvUserNick.setText(videoBean.getNickname());
        holder.tvDate.setText(videoBean.getDateline());
        holder.tvDescribe.setText(videoBean.getContent());
        holder.tvEvaluate.setText(videoBean.getComments());

        if (myItemClickListener != null){

            holder.tvEvaluate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    myItemClickListener.onEvaluateClick(position);
                }
            });

            holder.tvShare.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    myItemClickListener.onShareClick(position);
                }
            });
        }
    }

//    public Bitmap returnBitMap(String url){
//        URL myFileUrl = null;
//        Bitmap bitmap = null;
//        try {
//            myFileUrl = new URL(url);
//        } catch (MalformedURLException e) {
//            e.printStackTrace();
//        }
//        try {
//            HttpURLConnection conn = (HttpURLConnection) myFileUrl
//                    .openConnection();
//            conn.setDoInput(t);
//            conn.connect();
//            InputStream is = conn.getInputStream();
//            bitmap = BitmapFactory.decodeStream(is);
//            is.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return bitmap;
//    }


    @Override
    public int getItemCount() {
        return dataList.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder {

//        ImageView ivPost;

        JCVideoPlayerStandard video;
        TextView tvEvaluate,tvShare,tvUserNick,tvDate,tvDescribe;
        CircleImageView ivUserPic;

        public MyViewHolder(View itemView) {
            super(itemView);

            video = (JCVideoPlayerStandard) itemView.findViewById(R.id.video_player);
//            ivPost = (ImageView) itemView.findViewById(R.id.iv_video_post);
            tvEvaluate = (TextView) itemView.findViewById(R.id.tv_evaluate);
            tvShare = (TextView) itemView.findViewById(R.id.tv_share);
            tvUserNick = (TextView) itemView.findViewById(R.id.tv_user_nick);
            tvDate = (TextView) itemView.findViewById(R.id.tv_date);
            tvDescribe = (TextView) itemView.findViewById(R.id.tv_describe);
            ivUserPic = (CircleImageView) itemView.findViewById(R.id.iv_user_pic);

        }
    }
}
